package com.glor.operation.common.web.security.handler;

import com.glor.operation.common.user.dto.UserDTO;
import com.glor.operation.common.user.service.UserService;
import com.glor.operation.common.utils.RedisCacheHelper;
import com.glor.operation.common.web.security.userdetails.DefaultUserDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by heylear.cai on 2016/2/18.
 */
@Slf4j
public class DefaultAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        DefaultUserDetails user = (DefaultUserDetails) authentication.getPrincipal();
        log.info("========用户名-[{}], 用户ID-[{}]登陆成功========", user.getUsername(), user.getUserId());
        setDefaultTargetUrl("/");
        setAlwaysUseDefaultTargetUrl(true);
        super.onAuthenticationSuccess(request, response, authentication);
    }
}